<template>
  <!--  供应商对账-->
  <div class="salesMargin">
    <div class="header">
      <search
        :options="selectData"
        :search-son="searchSon"
        :msg="tacitly"
        @seek="collect"
        @reload="getData"
        @hand="handleQuery()"
      />
      <el-button type="primary" class="btn" size="mini" @click="handleQuery">
        搜索
      </el-button>
      <el-button
        class="btn1"
        type="primary"
        size="mini"
        @click="handleExport"
      >
        导出Excel
      </el-button>
      <filtration
        class="btn1"
        :ftype="ftype"
        :table-header="tableHeader1"
        :detail-header="tableHeader"
        :select-data="selectData"
        @seachData="handleQuery"
      />
    </div>
    <div class="table-content">
      <jc-table
        :table-data="tableData"
        :table-header="tableHeader"
        :table-height="tableHeight"
        serial
        show-summary
        :get-summaries="getSummaries"
      >
        <template v-slot:billSlot="col">
          <el-image
            class="productImg"
            :src="$img + col.scope.row.fimg"
            :preview-src-list="[$img + col.scope.row.fimg]"
          />
        </template>
        <template v-slot:tagSlot="col">
          <el-link type="primary" @click="jumpMateriel(col.scope.row)">{{ col.scope.row.fnumber }}</el-link>
        </template>
      </jc-table>
    </div>
    <!--    分页器-->
    <div class="footer">
      <jc-pagination
        v-show="total > 0"
        :total="total"
        :page.sync="currentPage"
        :limit.sync="size"
        @pagination="getData"
      />
    </div>
  </div>
</template>

<script>
import searData from '@/components/Search/mixin'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'
import filtration from '@/components/filtration'
import { queryNXSalPurGrossList, querySalPurGrossList } from '@/api/financial/salesMargin'
import search from '@/components/Search'

export default {
  components: {
    search,
    filtration
  },
  mixins: [searData],
  props: {
    msg: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      conceal: 1,
      ftype: 87,
      val: '',
      searInput: {},
      total: 0, // 总条目
      currentPage: 1, // 当前页
      size: 200, // 每页显示多少条数据
      tableHeight: '73vh',
      // 表头
      tableData: [], // 销售数据
      tableHeader: [
        { label: this.$t('shop.image'), type: 'bill', prop: 'fimg' },
        { label: this.$t('market.placedate'), prop: 'fdate' },
        { label: this.$t('market.order'), prop: 'xsfbillno', width: '120' },
        { label: this.$t('market.purOrderNo'), prop: 'ccfbillno', width: '120' },
        { label: this.$t('market.supplier'), prop: 'fsupplier', width: '140' },
        { label: this.$t('market.paezText1'), prop: 'f_paez_text1', width: '120' },
        { label: this.$t('market.custname'), prop: 'fcust', width: '140' },
        { label: this.$t('market.fmaterial'), type: 'tag', prop: 'fnumber', width: '120' },
        { label: this.$t('shop.fname2'), prop: 'fname', width: '120' },
        { label: this.$t('market.fmodel'), prop: 'fmodel', width: '120' },
        { label: this.$t('shop.fspecificaTion'), prop: 'fspecification', width: '120' },
        { label: this.$t('market.fqty2'), prop: 'xxfqty' },
        { label: this.$t('market.xxprice'), prop: 'xxftaxprice' },
        { label: this.$t('market.salerAmount'), prop: 'xxfallamount' },
        { label: this.$t('market.salCurrency'), prop: 'xxbb' },
        { label: this.$t('market.purchaserQty'), prop: 'ccfqty' },
        { label: this.$t('market.ccprice'), prop: 'ccftaxprice' },
        { label: this.$t('market.ccAmount'), prop: 'ccfallamount' },
        { label: this.$t('market.currency2'), prop: 'fsettlecurr' },
        { label: this.$t('market.mlr'), prop: 'ml' },
        { label: this.$t('market.mll'), prop: 'mll' },
        { label: this.$t('market.country'), prop: 'fcountry' },
        { label: this.$t('market.fisFree'), prop: 'fisfree' },
        { label: this.$t('market.fdescripTion'), prop: 'fdescription', width: '200' }
      ],
      tableHeader1: []
    }
  },
  created() {
    // this.getData()
    this.tableHeader1 = this.tableHeader
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 230px)' : '73vh'
  },
  methods: {
    // 导出表格
    handleExport() {
      require.ensure([], () => {
        const tHeader = this.tableHeader.map(res => {
          return res.label
        })
        const filterVal = this.tableHeader.map(res => {
          return res.prop
        })
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else {
          querySalPurGrossList({
            pageSize: 100000,
            pageNum: 1,
            search: this.searCollData.concat(this.details)
          }).then(res => {
            if (res.code === 0) {
              const list = res.data.array
              const data = formatJson(filterVal, list)
              export_json_to_excel(tHeader, data, '销售毛利报表')
            } else {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
        }
      })
    },
    // 获取销售毛利列表数据
    async getData(ev) {
      this.details = []
      if (ev && !ev.page) {
        this.details = ev
      }
      const DATA = {
        pageNum: this.currentPage,
        pageSize: this.size,
        search: this.searCollData.concat(this.details)
      }
      const api = this.msg === 0 ? querySalPurGrossList : queryNXSalPurGrossList
      await api(DATA).then(res => {
        if (res.code === 0) {
          this.tableData = res.data.array
          this.total = res.data.total
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 搜索
    handleQuery(ev) {
      this.currentPage = 1
      if (ev && ev.fname) {
        this.customHandle(ev)
        this.getData(ev.detail)
      } else {
        this.getData()
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.salesMargin {
  @include listBom;
  padding: 0;
  .productImg {
    max-width: 50px;
    max-height: 30px;
  }
  .header {
    position: relative;
    margin-bottom: 10px;
    .btn {
      transform: translateY(32%);
      margin-left: 470px;
    }
    .btn1 {
      transform: translateY(32%);
    }
  }
}
</style>
